﻿using Microsoft.AspNetCore.Mvc;
using NetTaste;
using WMS.Common.WMS.Common;
using WMS.Service.IService;

namespace wmsWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LoginController
    {
        private readonly ILogger<LoginController> _logger;
        private readonly ILogService _logService;

        public LoginController(ILogger<LoginController> logger, ILogService logService)
        {
            _logger = logger;
            _logService = logService;
        }

        /// <summary>
        /// 账号登录
        /// </summary>
        [HttpGet("AccountLogin")]
        public async Task<ResultApi> AccountLogin(string account, string password)
        {
            try
            {
                if (string.IsNullOrEmpty(account))
                {
                    return ResultHelper.BadRequest("账号不能为空！");
                }
                if (string.IsNullOrEmpty(password))
                {
                    return ResultHelper.BadRequest("密码不能为空！");
                }

                var (success, message, obj) = await _logService.AccountLoginAsync(account, password);

                if (success)
                {
                    return ResultHelper.Success(obj, message);
                }
                else
                {
                    return ResultHelper.BadRequest(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"账号登录失败 - 账号: {account}");
                return ResultHelper.Error("500", "登录服务器错误");
            }
        }

        /// <summary>
        /// 发送邮箱验证码
        /// </summary>
        [HttpGet("EmailCode")]
        public async Task<ResultApi> EmailCode(string email)
        {
            try
            {
                if (string.IsNullOrEmpty(email))
                {
                    return ResultHelper.BadRequest("邮箱不能为空！");
                }

                var (success, message) = await _logService.EmailCodeAsync(email);

                if (success)
                {
                    return ResultHelper.Success(message);
                }
                else
                {
                    return ResultHelper.BadRequest(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"发送邮箱验证码失败 - 邮箱: {email}");
                return ResultHelper.Error("500", "服务器错误");
            }
        }

        /// <summary>
        /// 邮箱登录
        /// </summary>
        [HttpGet("EmailLogin")]
        public async Task<ResultApi> EmailLogin(string email, string code)
        {
            try
            {
                if (string.IsNullOrEmpty(email))
                {
                    return ResultHelper.BadRequest("邮箱不能为空！");
                }
                if (string.IsNullOrEmpty(code))
                {
                    return ResultHelper.BadRequest("验证码不能为空！");
                }

                var (success, message, obj) = await _logService.EmailLoginAsync(email, code);

                if (success)
                {
                    return ResultHelper.Success(obj, message);
                }
                else
                {
                    return ResultHelper.BadRequest(message);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"邮箱登录失败 - 邮箱: {email}");
                return ResultHelper.Error("500", "服务器错误");
            }
        }
    }
}